package com.zrx.app.android.fragment;

import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONObject;
import com.zrx.app.android.R;

import java.util.Map;

/**
 * Created by liuji on 2017/5/4.
 *
 * 审核资料录入里面的附加信息页面
 */

public class AdditionalDataFrag extends BaseFragment {

    EditText text_keyName;
    LinearLayout ll;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View _view = inflater.inflate(R.layout.fragment_additional_data, container, false);
        ll = (LinearLayout) _view.findViewById(R.id.dyn_content);
        text_keyName = (EditText) _view.findViewById(R.id.text_keyName);
        Button btn_addText = (Button) _view.findViewById(R.id.btn_addText);
        Button btn_addPhoto = (Button) _view.findViewById(R.id.btn_addPhoto);
        Button btn_addVideo = (Button) _view.findViewById(R.id.btn_addVideo);

        for (Map.Entry<String, Object> obj : node.entrySet()) {
            String key = obj.getKey();
            if (obj.getValue() instanceof JSONObject) {
                JSONObject innerNode = (JSONObject) obj.getValue();
                if (innerNode.containsKey("type")) {
                    String type = innerNode.getString("type");
                    BaseFragment frag = null;
                    if ("字段".equals(type)) {
                        frag = new TextFrag();
                    } else if ("照片".equals(type)) {
                        frag = new ImageFrag();
                    } else if ("视频".equals(type)) {
                        frag = new VideoFrag();
                    }
                    if (frag != null) {
                        frag.initParams(innerNode,key,dir);
                        FragmentManager fm = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fm.beginTransaction();
                        fragmentTransaction.add(ll.getId(), frag);
                        fragmentTransaction.commit();
                    }
                }
            }
        }


        btn_addText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addMore("字段");
            }
        });

        btn_addPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addMore("照片");
            }
        });

        btn_addVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addMore("视频");
            }
        });

        return _view;
    }

    private void addMore(String type) {     //判断内容是否为空
        String keyName = text_keyName.getText().toString();
        BaseFragment frag = null;
        JSONObject innerNode=new JSONObject();
        innerNode.put("type",type);


        if (keyName == null || "".equals(keyName)) {
            new AlertDialog.Builder(getContext())
                    .setTitle("警告信息")
                    .setMessage("名称不能为空")
                    .setPositiveButton("确定", null)
                    .show();
        } else {
            if ("照片".equals(type)) {
                if (!keyName.contains("照片")) {
                    keyName += "照片";
                }
            } else if ("视频".equals(type)) {
                if (!keyName.contains("视频")) {
                    keyName += "视频";
                }
            }
            if (node.containsKey(keyName)) {
                new AlertDialog.Builder(getContext())
                        .setTitle("警告信息")
                        .setMessage("名称已存在")
                        .setPositiveButton("确定", null)
                        .show();
            } else {
                if("字段".equals(type)) {
                    frag=new TextFrag();
                } else if ("照片".equals(type)) {
                    frag = new ImageFrag();
                } else if ("视频".equals(type)) {
                    frag=new VideoFrag();
                }

                if(frag!=null) {
                    frag.initParams(innerNode,keyName,dir);
                    text_keyName.setText("");
                    node.put(keyName, innerNode);
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fm.beginTransaction();
                    fragmentTransaction.add(ll.getId(), frag);
                    fragmentTransaction.commit();
                }
            }
        }
    }
}
